
//
//  HeaderTapViewController.m
//  wuliangye
//
//  Created by jarze on 16/2/18.
//  Copyright © 2016年 jarze. All rights reserved.
//

#import "HeaderTapViewController.h"
#import "ClassModel.h"
#import "ClassHeaderView.h"
#import "GCD/GCD.h"

static NSString *infocell = @"BaseTableViewCell";
static NSString *headerFlag = @"ClassHeaderView";

@interface HeaderTapViewController ()<UITableViewDelegate, UITableViewDataSource,CustomHeaderFooterViewDelegate>
@property (nonatomic, strong) NSMutableArray *classModel;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic) BOOL sectionFirstLoad;
@end

@implementation HeaderTapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self initTableView];
    [self firstLoadDataAnimation];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)initData
{
    NSArray *datas = @[@{@"className" : @"Aitna",
                         @"students"  : @[@{@"name" : @"Y.X.M.",   @"age" : @(27)},
                                          @{@"name" : @"Leif",     @"age" : @(12)},
                                          @{@"name" : @"Lennon",   @"age" : @(23)},
                                          @{@"name" : @"Lambert",  @"age" : @(19)},
                                          @{@"name" : @"Jerome",   @"age" : @(19)},
                                          @{@"name" : @"Isidore",  @"age" : @(19)}]},
                       @{@"className" : @"Melete",
                         @"students"  : @[@{@"name" : @"Merle",    @"age" : @(34)},
                                          @{@"name" : @"Paddy",    @"age" : @(31)},
                                          @{@"name" : @"Perry",    @"age" : @(10)},
                                          @{@"name" : @"Philip",   @"age" : @(39)}]},
                       @{@"className" : @"Aoede",
                         @"students"  : @[@{@"name" : @"Verne",    @"age" : @(34)},
                                          @{@"name" : @"Vincent",  @"age" : @(78)},
                                          @{@"name" : @"Walter",   @"age" : @(43)},
                                          @{@"name" : @"Zachary",  @"age" : @(21)}]},
                       @{@"className" : @"Dione",
                         @"students"  : @[@{@"name" : @"Timothy",  @"age" : @(98)},
                                          @{@"name" : @"Roderick", @"age" : @(21)},
                                          @{@"name" : @"Quentin",  @"age" : @(7)},
                                          @{@"name" : @"Paddy",    @"age" : @(67)}]},
                       @{@"className" : @"Adanos",
                         @"students"  : @[@{@"name" : @"Mortimer", @"age" : @(23)},
                                          @{@"name" : @"Michael",  @"age" : @(45)},
                                          @{@"name" : @"Kevin",    @"age" : @(89)},
                                          @{@"name" : @"Jeremy",   @"age" : @(23)}]},];

    if (_classModel == nil) {
        _classModel = [NSMutableArray array];
        
    }
    
    for (int i = 0; i < datas.count; i ++) {
        ClassModel *classModel = [[ClassModel alloc] initWithDictionary:datas[i]];
        classModel.expend = NO;
        [self.classModel addObject:classModel];
    }
    
    
}

- (void)initTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:infocell];
    [self.tableView registerClass:[ClassHeaderView class] forHeaderFooterViewReuseIdentifier:headerFlag];
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.sectionFirstLoad == NO) {
        
        return 0;
        
    } else {
        
        return self.classModel.count;
    }
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ClassModel *model = _classModel[section];
    
    if (model.expend == YES) {
        
        return [model.students count];
        
    } else {
        
        return 0;
    }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:infocell];
    ClassModel *classModel = _classModel[indexPath.section];
    NSArray *ary = classModel.students;
    cell.textLabel.text = ary[indexPath.row][@"name"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", ary[indexPath.row][@"age"]];
    return  cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ClassModel *classModel = _classModel[section];
    
    ClassHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerFlag];
    headerView.delegate = self;
    headerView.data = classModel;
    headerView.section = section;
    [headerView loadContent];
    if (classModel.expend == YES) {
        
        [headerView extendStateAnimated:NO];
        
    } else {
        
        [headerView normalStateAnimated:NO];
    }
    
  
    return headerView;

    
}

-  (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
 
    return 30;
}
- (void)customHeaderFooterView:(ClassHeaderView *)customHeaderFooterView event:(id)event
{
    NSInteger section = customHeaderFooterView.section;
    ClassModel *model = _classModel[section];
    ClassHeaderView *classHeaderView = (ClassHeaderView *)customHeaderFooterView;
    if (model.expend == YES) {
        
        // 缩回去
        model.expend = NO;
        [classHeaderView normalStateAnimated:YES];
        
        NSMutableArray *indexPaths = [NSMutableArray array];
        for (int i = 0; i < model.students.count; i++) {
            
            [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:section]];
        }
        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
        
    } else {
        
        // 显示出来
        model.expend = YES;
        [classHeaderView extendStateAnimated:YES];
        
        
        NSMutableArray *indexPaths = [NSMutableArray array];
        for (int i = 0; i < model.students.count; i++) {
            
            [indexPaths addObject:[NSIndexPath indexPathForItem:i inSection:section]];
        }
        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    }
  
}

- (void)firstLoadDataAnimation {
    
    [GCDQueue executeInMainQueue:^{
        
        // Extend sections.
        self.sectionFirstLoad = YES;
        NSIndexSet *indexSet  = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.classModel.count)];
        [self.tableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
        
    } afterDelaySecs:0.3f];
}

@end


